package com.rzico.sbl.ui.mine

import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityWalletBinding
import com.rzico.sbl.viewmodel.WalletViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.floatValue
import com.xinnuo.common.helper.intValue
import com.xinnuo.common.helper.startIncreaseAnimator
import com.xinnuo.common_ui.base.BaseActivity
import org.json.JSONObject

class WalletActivity : BaseActivity(R.layout.activity_wallet) {

    private val mBinding by lazy { ActivityWalletBinding.bind(getRootView()) }
    private var mCardId = ""

    override fun getViewModel() = get<WalletViewModel>()

    override fun initData() {
        setToolbarVisibility(false)
    }

    override fun onStart() {
        super.onStart()

        getViewModel().wallet()
        getViewModel().walletCard()
    }

    override fun initListener() {
        mBinding.apply {
            walletCharge.oneClick {
//                startActivityEx<WalletChargeActivity>(
//                    "balance" to walletAmount.textString()
//                )
            }

            walletDraw.oneClick {
                startActivityEx<WalletDrawActivity>(
                    "amount" to walletAmount.content
                )
            }
            walletCard.oneClick { startActivityEx<WalletBankActivity>() }
            walletBill.oneClick { startActivityEx<WalletBillActivity>() }
            walletGold.oneClick {
                mCardId.notEmpty {
                    startActivityEx<WalletCoinActivity>("cardId" to this)
                }
            }

            // 观察钱包信息
            getViewModel()
                .mBalanceEvent
                .observe(getLifecycleOwner()) {
                    val obj = JSONObject(it)

                    walletAmount.startIncreaseAnimator(
                        to = obj.optNotNull("balance").floatValue,
                        milliseconds = 300
                    )

                    val bindStatus = obj.optBoolean("binded")
                    walletCard.setRightString(conditionIf(bindStatus, "已绑定", "未绑定"))
                }

            // 观察会员卡信息
            getViewModel()
                .mCoinEvent
                .observe(getLifecycleOwner()) {
                    val obj = JSONObject(it)

                    mCardId = obj.optNotNull("id")
                    walletCoin.startIncreaseAnimator(
                        to = obj.optNotEmpty("point", "0").intValue,
                        milliseconds = 300
                    )
                }
        }
    }

}